const {
  MemberModel
} = require('../../../utils/member-model.js');
var utils =
  require("../../../utils/util.js");
var memberModel = new MemberModel();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    src: 'https://oss.kiscloud.net/wechat/images/no_phone.png',
    integral: 0, //积分
    memberName: "", //会员名称
    memberId: wx.getStorageSync('memberId'), //会员id
    total: 0, //合计
    title: "",
    image: "",
    gameData: [], //游戏数据
    gameType: "", //currency游戏币，ticket游戏券
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var htmlType = options.type; //currency游戏币，ticket游戏券
    this.getUserInfo(); //微信会员信息
    this.getUserAssetSummary(); //会员资产汇总
    this.setData({
      gameType: htmlType,
      title: htmlType == "currency" ? "总币数" : htmlType == "ticket" ? "总局数" : "",
      image: htmlType == "currency" ? "https://oss.kiscloud.net/wechat/images/icon_game_currency.png" : htmlType == "ticket" ? "https://oss.kiscloud.net/wechat/images/icon_game_ticket.png" : ""
    });
    wx.setNavigationBarTitle({
      title: htmlType == "currency" ? "游戏币" : htmlType == "ticket" ? "游戏券" : ""
    })
    if (htmlType == "currency") { //游戏币
      this.getUserAssetDetail(); //游戏币会员资产详情
    } else if (htmlType == "ticket") { //游戏券
      this.getUserGameCoupon(); //游戏券会员资产详情
    }
  },

  /**
   * 会员资产汇总
   */
  getUserAssetSummary: function () {
    memberModel.userAssetSummary({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      var that = this;
      that.setData({
        integral: utils.formatCurrency(data.integralTotal),
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   *微信会员信息
   */
  getUserInfo: function () {
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      var that = this;
      that.setData({
        memberName: data.nickname || "", //会员名称
        src: data.avatarUrl || "https://oss.kiscloud.net/wechat/images/no_phone.png", //会员头像
        memberId: data.memberId ? data.memberId : "", //会员ID
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 游戏币会员资产详情
   */
  getUserAssetDetail() {
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId');
    param.assetType = 0; //游戏币
    param.assetStatus = 0; //0:正常 3:过期
    memberModel.userAssetDetail(param).then(data => {
      this.setData({
        total: utils.formatCurrency(data.assetAvail) || 0,
      })
      this.setDetailData(data);
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 游戏券会员资产详情
   */
  getUserGameCoupon() {
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId');
    param.assetStatus = 0; //0:正常 3:过期
    memberModel.userGameCoupon(param).then(data => {
      this.setData({
        total: utils.formatCurrency(data.assetAvail) || 0,
      })
      this.setDetailData(data);
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   *编辑数据
   */
  setDetailData(data) {
    var that = this;
    var dataList = [];
    if (data && data.assetShopList && data.assetShopList.length > 0) {
      for (var i = 0; i < data.assetShopList.length; i++) {
        var item = data.assetShopList[i];
        var list = {};
        list.partnerName = item.partnerName;
        list.shopId = item.shopId;
        list.shopName = item.shopName;
        list.assetAvail = utils.formatCurrency(item.assetAvail);
        dataList.push(list);
      }
      this.setData({
        gameData: dataList
      })
    }
  },

  //跳转到积分明细
  goIntegralDetail() {
    wx.navigateTo({
      url: '/pages/memberCenter/integralDetail/index'
    })
  },

  //跳转到个人资料
  goInformation: function () {
    wx.navigateTo({
      url: '/pages/memberCenter/information/personalData/index'
    })
  },
  /**
   * 跳转到游戏券详情
   */
  goGameInfo: function (e) {
    var shopId = e.currentTarget.dataset.id; //门店id
    var cardNo = e.currentTarget.dataset.code; //卡号
    if (this.data.gameType == "ticket") { //券
      wx.navigateTo({
        url: '/pages/memberCenter/gameTicket/index?shopId=' + shopId
      })
    } else if (this.data.gameType == "currency") { //币
      wx.navigateTo({
        url: '/pages/memberCenter/expenditureInfo/index?shopId=' + shopId + '&cardNo=' + cardNo
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})